package BTChat;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import java.util.Date;

public class BTChat extends MIDlet implements CommandListener 
{
    //<editor-fold defaultstate="collapsed" desc=" Pola klasy ">
    public Form form;
    public String[] elements;
    public List menu;
    public Date time = new Date();
    public String nick;
    public Display display = Display.getDisplay(this);
    public boolean debugeMode = true;
    private final Command EXIT_CMD = new Command("Exit", Command.EXIT, 2);
    private final Command SAVE_CMD = new Command("Ok", Command.SCREEN, 1);
    private final Command SEND_CMD = new Command("Send", Command.SCREEN, 1);
    private final Command ROUT_CMD = new Command("Send", Command.SCREEN, 2);
    private final Command CONNECT_CMD = new Command("Connect", Command.OK, 2);
    private final Command NEW_CMD = new Command("Utwórz kanał", Command.SCREEN, 1);
    private final Command RESEARCH_CMD = new Command("Przeszukaj ponownie", Command.SCREEN, 2);
    private BTChatBT BTChatBT;
    private TextBox tekst;
    private Alert error;
    private TextBox nick_form;
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc=" Konstruktor BTChat() ">
    public BTChat() 
    {
	nick_form = new TextBox("Twój Nick", "", 30, TextField.ANY);
	nick_form.addCommand(SAVE_CMD);
	nick_form.setCommandListener(this);
	form = new Form("CZATERIA.TicTaria.pl");
	form.addCommand(EXIT_CMD);
	form.addCommand(SEND_CMD);
	form.setCommandListener(this);
	tekst = new TextBox("Wpisz tekst", "", 255, TextField.ANY);
	tekst.addCommand(ROUT_CMD);
	tekst.setCommandListener(this);
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Interfejs MIDlet ">
    public void startApp() {Display.getDisplay(this).setCurrent(nick_form);}

    public void pauseApp() {}

    public void destroyApp(boolean unconditional) {}
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc=" Obsluga zdarzeń commandAction() ">
    public void commandAction(Command c, Displayable s) 
    {
	if (c == EXIT_CMD) 
	{
	    this.notifyDestroyed();
	}
	//przypadek serwera
	if (c == SAVE_CMD)
	{
	    nick = nick_form.getString();
	    BTChatBT = new BTChatBT(this);
	}
	
	if (c == SEND_CMD)
	{
	    this.tekst.setString("");
	    display.setCurrent(tekst);
	}
	if (c == NEW_CMD)
	{
	    this.BTChatBT.searchOnly = false;
	    this.BTChatBT.startSerwer();
	    display.setCurrent(form);
	}
	if (c == CONNECT_CMD)
	{
	    this.BTChatBT.w = new Thread(BTChatBT);
	    this.BTChatBT.w.start();
	    display.setCurrent(form);
	}
	if (c == RESEARCH_CMD)
	{
	    this.BTChatBT.searchOnly = true;
	    this.BTChatBT.w = new Thread(BTChatBT);
	    this.BTChatBT.w.start();
	    display.setCurrent(form);
	}
	if (c == ROUT_CMD)
	{
	    message message = new message(tekst.getString(), nick, time);
	    try
	    {
		BTChatBT.send(message);
	    }
	    catch (Exception e)
	    {
		if(debugeMode == true)
		{
		    this.error = new Alert("Błąd krytyczny", e.toString(), null,AlertType.ERROR);
		    this.display.setCurrent(error);
		}
	    }
	    form.append(message.show());
	    display.setCurrent(form);
	}
    }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc=" Zakończenie poszukiwań searchFinished() ">
    public void searchFinished()
    {
	if(this.elements.length == 0)
	{
	    String[] el = {"nie znaleziono urzadzen w poblizu", "jeszcze raz"};
	    this.elements = el;
	    menu = new List("BTChat", List.IMPLICIT,el, null);
	    menu.addCommand(RESEARCH_CMD);
	    menu.addCommand(NEW_CMD);
	}
	else
	{
	    menu = new List("BTChat", List.IMPLICIT,elements, null);
	    menu.addCommand(CONNECT_CMD);
	}
	menu.setCommandListener(this);
	display.setCurrent(menu);
    }
    //</editor-fold>
}